package cn.ycl.system.other.study.designMode;

import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.spring.SpringUtil;
import lombok.Builder;
import lombok.Data;

/**
 * @author YuanChangLiang
 * @description
 * @date created in 14:53 2021/6/24
 */

@Data
@Builder
public class User implements Cloneable {
    private String name;
    private int age;


    //重写克隆方法：调用的是父类默认的clone方法来拷贝user类
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Object user = null;
        try {
            user = super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return user;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        User user = User.builder().name("克隆").age(25).build();
        System.out.println(user.getName());
        User clone = (User) user.clone();
        System.out.println(clone.getName());
        clone.setName("新的");
        System.out.println("====================改变name后==================");
        System.out.println("克隆后的clong的name：" + clone.getName());
        System.out.println("克隆后的user的name：" + user.getName());
        System.out.println(DateUtil.ageOfNow("19970428"));

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static <T> T getBean(Class<T> clazz) {
        return SpringUtil.getBean(clazz);
    }
}



